package com.dudetest.data;

import java.util.logging.Logger;

import com.dudetest.data.objectify.DAOBase;
import com.dudetest.data.objectify.independent.DAOObjectifyIndependent;
import com.dudetest.data.objectify.subclassed.DAOObjectifySubclassed;
import com.googlecode.objectify.ObjectifyFactory;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;

public class DAOFactory {
	protected static final Logger log = Logger.getLogger(DAOFactory.class.getName());
	private static DAOBase dao;
	
	public static DAOBase getDAO(DAOVersion version, boolean memcache){
		if(dao==null || dao.getVersion()!=version || dao.hasGlobalCache()!=memcache){
			switch(version){
			case INDEPENDENT:
				dao = makeDAOObjectifyIndependent(memcache);
				log.info("Make a Objectify Independent DAO");
				break;
			case SUBCLASSED:
				dao = makeDAOObjectifySubclassed(memcache);
				log.info("Make a Objectify Subclassed DAO");
				break;
			}
		}
		return dao;
	}
	
	public static ObjectifyFactory fact(){
		return ObjectifyService.factory();
	}

	private static DAOObjectifyIndependent makeDAOObjectifyIndependent(boolean memcache){
		return new DAOObjectifyIndependent(new ObjectifyOpts().setGlobalCache(memcache).setSessionCache(false));
	}

	private static DAOObjectifySubclassed makeDAOObjectifySubclassed(boolean memcache){
		return new DAOObjectifySubclassed(new ObjectifyOpts().setGlobalCache(memcache).setSessionCache(false));
	}
	
}
